{extend name="common/base" /}
{block name="body"}
<div style="padding: 0px 10px 0px 10px;">
<div class="layui-tab">
    <ul class="layui-tab-title">
        <!--<li {eq name="type" value="index"}class="layui-this"{/eq}> <a href="{:url('index')}">在使用</a></li>-->
        <li {eq name="type" value="uninstall"}class="layui-this"{/eq}> <a href="{:url('index',['type'=>'uninstall'])}">退出|还原</a></li>
        <li {eq name="type" value="notinstall"}class="layui-this"{/eq}><a href="{:url('index',['type'=>'notinstall'])}">安装|卸载</a></li>
    </ul>
</div>
{switch name="type"}
{case value="index"}
<table class="layui-table" lay-even lay-skin="nob">
    <thead>
    <tr>
        <th width="60">应用Logo</th>
        <th width="60">应用名称</th>
        <th>应用标识</th>
        <!--<th>简介</th>-->
        <th>版本</th>
        <th>新版本</th>
        <th>作者</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {volist name="addons" id="vo"}
    <tr>
        <td><img width="30" height="30" src="{$vo.logo}"></td>
        <td>{$vo.name}</td>
        <td>{$vo.addon}</td>
        <!--<td>{$vo.desc}</td>-->
        <td>{$vo.version}</td>
        <td>{:getAddonConfigByFile($vo['addon'],'version')}</td>
        <td>{$vo.author}</td>
        <td>
            <a class="layui-btn layui-btn-xs layui-btn-normal" href="{:url('mp/App/index',['name'=>$vo.addon,'type'=>'news','mid'=>$mpInfo.id])}">进入</a>
            {eq name="$vo.upgrade" value="1"}
            <a class="layui-btn layui-btn-xs layui-btn-warm" href="javascript:;" onclick="upgrade('{$vo.addon}')">升级</a>
            {/eq}
        </td>
    </tr>
    {/volist}
    </tbody>
</table>
{/case}
    {case value="uninstall"}
    <table class="layui-table" lay-even lay-skin="nob">
        <thead>
        <tr>
            <th>应用Logo</th>
            <th>应用名称</th>
            <th>应用标识</th>
            <!--<th>简介</th>-->
            <th>版本</th>
            <th>新版本</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {volist name="addons" id="vo"}
        <tr>
            <td><img width="30" height="30" src="{$vo.logo}"></td>
            <td>{$vo.name}</td>
            <td>{$vo.addon}</td>
            <!--<td>{$vo.desc}</td>-->
            <td>{$vo.version}</td>
            <td>{:getAddonConfigByFile($vo['addon'],'version')}</td>
            <td>{$vo.author}</td>
            <td width="90">
                <button class="layui-btn layui-btn-xs" onclick="closeApp('{$vo.addon}')">退出</button>
                <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="wipeDataApp('{$vo.addon}')">还原</button>
            </td>
        </tr>
        {/volist}
        </tbody>
    </table>
    {/case}
{case value="notinstall"}
<table class="layui-table" lay-even lay-skin="nob">
    <thead>
    <tr>
        <th>应用Logo</th>
        <th>应用名称</th>
        <th>应用标识</th>
        <!--<th>简介</th>-->
        <th>版本</th>
        <th>新版本</th>
        <th>作者</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {volist name="addons" id="vo"}
    <tr >
        <td><img width="30" height="30" src="{:getAddonLogo($vo['addon'])}"></td>
        <td>{$vo.name}</td>
        <td>{$vo.addon}</td>
        <!--<td>{$vo.desc}</td>-->
        <td>{$vo.version}</td>
        <td>{:getAddonConfigByFile($vo['addon'])}</td>
        <td>{$vo.author}</td>
        <td width="110"><button class="layui-btn layui-btn-xs " onclick="installApp('{$vo.addon}')">安装|启用</button>
            <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="uninstallApp('{$vo.addon}')">卸载</button>
        </td>
    </tr>
    {/volist}
    </tbody>
</table>
{/case}
{/switch}
</div>
<script>
    var layer;
    layui.use('layer', function(){
        var layer = layui.layer;
    });
    function installApp(name) {
            var loadIndex = layer.load(1)
            $.post("{:url('admin/app/install')}",{'name':name},function (res) {
                if(res.status=='0'){
                    layer.close(loadIndex)
                    layer.alert(res.msg);
                }
                if(res.status=='1'){
                    layer.msg(res.msg,{time:1000},function () {
                        location.href="{:url('admin/app/index',['type'=>'notinstall'])}";
                    });
                }
            }).error(function (error) {
                layer.alert('500 Internal Server Error');
                layer.close(loadIndex)
            })
            return false;

    }
    function upgrade(name) {
            $.post("{:url('admin/app/upgrade')}",{'name':name},function (res) {
                if(res.status=='0'){
                    layer.alert(res.msg);
                }
                if(res.status=='1'){
                    layer.msg(res.msg,{time:1000},function () {
                        location.href="{:url('admin/app/index')}";
                    });
                }
            })
    }
    function closeApp(name) {
            $.post("{:url('admin/app/close')}",{'name':name},function (res) {
                if(res.status=='0'){
                    layer.alert(res.msg);
                }
                if(res.status=='1'){
                    layer.msg(res.msg,{time:1000},function () {
                        location.href="{:url('admin/app/index',['type'=>'uninstall'])}";
                    });
                }
            })
    }

    function uninstallApp(name) {
        layer.confirm('你确定卸载（'+name+'）此应用吗？点击确定将会清空此应用所有数据以及文件', {
            btn: ['确定','不的']
        }, function(load_index){
            layer.close(load_index);
            var index = layer.load(1,{
                shade: [0.1,'#000']
            });
            $.post("{:url('admin/app/uninstall')}",{'name':name},function (res) {
                if(res.status=='0'){
                    layer.close(index)
                    layer.alert(res.msg);
                }
                if(res.status=='1'){
                    layer.msg(res.msg,{time:1000},function () {
                        location.href="{:url('admin/app/index',['type'=>'notinstall'])}";
                    });
                }
            }).error(function (error) {
                layer.alert('500 Internal Server Error');
                layer.close(index)
            })
        }, function(){

        });
    }
    function wipeDataApp(name) {
        layer.confirm('你确定还（'+name+'）此应用吗？点击确定将会清空数据以及还原未安装状态', {
            btn: ['确定','不的']
        }, function(load_index){
            layer.close(load_index);
            var index = layer.load(1,{
                shade: [0.1,'#000']
            });
            $.post("{:url('admin/app/wipeData')}",{'name':name},function (res) {
                if(res.status=='0'){
                    layer.close(index)
                    layer.alert(res.msg);
                }
                if(res.status=='1'){
                    layer.msg(res.msg,{time:1000},function () {
                        location.href="{:url('admin/app/index',['type'=>'uninstall'])}";
                    });
                }
            }).error(function (error) {
                layer.alert('500 Internal Server Error');
                layer.close(index)
            })
        }, function(){

        });
    }
</script>
{/block}